//package com.lx.pc.restful.interceptor;
//
//import java.util.Date;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.springframework.context.support.ClassPathXmlApplicationContext;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import com.lx.pc.domain.vo.user.UserCookieQo;
//import com.lx.pc.domain.vo.user.UserVo;
//import com.lx.pc.restful.redis.SpringRedisTemplate;
//import com.lx.pc.restful.usercookie.UserCookie;
//import com.lx.pc.restful.util.AppContextUtil;
//import com.lx.pc.service.user.IUserService;
//import com.lx.pc.utils.Constants;
//import com.lx.pc.utils.Utils;
//
//public class WsGlobalInterceptor implements HandlerInterceptor {
//
//	private String exceptionUrl;
//	
//	private String contextPath;
//
//	public void setExceptionUrl(String exceptionUrl) {
//		this.exceptionUrl = exceptionUrl;
//	}
//
//	public void setContextPath(String contextPath) {
//		this.contextPath = contextPath;
//	}
//
//	@Override
//	public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object obj, Exception e) throws Exception {
//
//	}
//
//	@Override
//	public void postHandle(HttpServletRequest req, HttpServletResponse res, Object obj, ModelAndView mav) throws Exception {
//
//	}
//
//	@Override
//	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
//		request.setCharacterEncoding("UTF-8");
//		response.setCharacterEncoding("UTF-8");
//		ClassPathXmlApplicationContext context = AppContextUtil.getInstance();
//		SpringRedisTemplate springRedisTemplate = context.getBean("springRedisTemplate", SpringRedisTemplate.class);
//		UserCookie userCookie = new UserCookie();
//		UserCookieQo userCookieQo = userCookie.getUserInfoCookie(request);
//		if (userCookieQo == null) {
//			// 判断用户访问URI是否需要登录
//			if (exceptionUrl.indexOf(request.getRequestURI().replaceAll(contextPath, "")) >= 0) {
//				return true;
//			} else {
//				// 重定向到登录页面
//				response.sendRedirect(request.getContextPath() + "/index.do");
//				return false;
//			}
//		} else {
//			String userId = userCookieQo.getId();
//			String loginName = userCookieQo.getUserName();
//			String password = userCookieQo.getPassword();
//			String sessionKey = "SESSION_" + userId;
//			
//			IUserService userInfoService = context.getBean("userService", IUserService.class);
//			
//			UserVo userVo = userInfoService.findByUsername(loginName);
//			if (userVo != null) {
//				String pass = Utils.md5(password, Constants.PWD_CACHE_KEY);
//	        	if (userVo.getPassword().equals(pass)) {
//	        		String lastLoginTime = String.valueOf(System.currentTimeMillis());
//	        		userCookieQo.setLastLoginTime(lastLoginTime);
//	        		
//					userCookie.addUserInfoCookie(response, userVo);
//	        	}
//			}
//			
////			if (springRedisTemplate.exists(sessionKey)) {
////				return true;
////			} else {
////				IUserInfoService userInfoService = context.getBean("userInfoService", IUserInfoService.class);
////				UserInfo userInfo = userInfoService.login(loginName, password);
////				if (userInfo != null && userInfo.getId().equals(userId)) {
////					userInfo = userInfoService.load(userId);
////					userInfo.setLastLoginTime(new Date());
////					userInfoService.update(userInfo);
////					springRedisTemplate.save(sessionKey, userId);
////					return true;
////				} else {
////					// 重定向到登录页面
////					response.sendRedirect(request.getContextPath() + "/archist.do");
////					return false;
////				}
//		}
//		return false;
//	}
//}
